$(function() {
	layui.use(['form', 'laypage', 'util', 'layer'], function() {

		var form = layui.form;
		form.render();
		var laypage = layui.laypage;
		var util = layui.util;
		var layer = layui.layer;
		var total;

		function find_sysUser_list(page, rows) {
			$.ajax({
				type: "post",
				url: "http://localhost:8080/ssmService/sysUser/selectPageUserVo",
				async: false,
				data: {
					"queryParams.curr_page": page,
					"queryParams.page_size": rows
				},
				"beforSend": function() {
					index = layer.load(1, {
						shade: [0.5, "black"]
					});
				},
				"success": function(result) {
					if(result.resultCode == 0) {
						build_sysUser_list(result);
						total = result.data.total;
					} else {
						layer.msg("服务器错误，请联系管理员", {
							icon: 1,
							time: 1000
						});
					}
				},
				"error": function() {
					layer.msg("请求失败,联系管理员", {
						icon: 2,
						time: 1000
					});
				}
			});
		}

		function build_sysUser_list(result) {
			var userList = result.data.list;
			console.log(userList)
			$(".table_tbody").empty();
			$.each(userList, function(index, item) {
				var tr = $("<tr></tr>");
				var td1 = $("<td></td>").addClass("childrenBox").append("<input type='checkbox' user_id='" + item.user_id + "' lay-skin='primary'>");
				var td2 = $("<td></td>").append(item.user.account);
				var td3 = $("<td></td>").append(item.user.name);
				var phone = "";
				if(item.user.phone != null) {
					phone = item.user.phone;
				}
				var td4 = $("<td></td>").append(phone);
				var address = "";
				if(item.user.address != null) {
					address = item.user.address;
				}
				var td5 = $("<td></td>").append(address);
				var birthday = "";
				if(item.user.birthday != null) {
					birthday = item.user.birthday;
				}
				var td6 = $("<td></td>").append(util.toDateString(birthday, "yyyy-MM-dd"));
				var td7 = "";
				if(item.user.state == 1) {
					td7 = $("<td></td>").append("<input type='checkbox' user_id='" + item.user_id + "' name='' lay-text='ON|OFF' lay-skin='switch' checked>");
				} else {
					td7 = $("<td></td>").append("<input type='checkbox' user_id='" + item.user_id + "' name='' lay-text='ON|OFF' lay-skin='switch'>");
				}

				var td8 = $("<td></td>").append("<a href='javacript:;' user_id='" + item.user_id + "' class='layui-btn layui-btn-sm updateUser'>编辑</a>")
					.append("<a href='javacript:;' user_id='" + item.user_id + "' class='layui-btn layui-btn-sm reset'>重置密码</a>")
					.append("<a href='javacript:;' user_id='" + item.user_id + "' class='layui-btn layui-btn-sm layui-btn-danger delete'>删除</a>");
				tr.append(td1).append(td2).append(td3).append(td4).append(td5).append(td6).append(td7).append(td8).appendTo(".table_tbody");
			});
			form.render();
		}

		find_sysUser_list(1, 10);
		laypage.render({
			elem: "table_page",
			count: total,
			layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'],
			jump: function(obj, first) {
				find_sysUser_list(obj.curr, obj.limit);

			}
		});

		$(document).on("click", ".delete", function() {
			var userId = $(this).attr("user_id");
			layer.confirm("你确定要删除该条信息吗?",function(){
					delete_userbByUserId(userId);
			},1000);
		

		});

		function delete_userbByUserId(userId) {
			$.ajax({
				url: "http://localhost:8080/ssmService/sysUser/deleteUser/" + userId,
				data: {
					"ids": userId
				},
				async: true,
				"success": function(result) {
					if(result.resultCode == 0) {
						layer.msg(result.message, {
							icon: 1,
							time: 1000
						});
						window.location.reload();
					} else {
						layer.msg(result.message, {
							icon: 2,
							time: 1000
						});
					}
				}
			});
		}

		form.on("checkbox(parentBox)", function(data) {
			if(data.elem.checked) {
				$(".childrenBox input[type='checkbox']").prop("checked", true);
			} else {
				$(".childrenBox input[type='checkbox']").prop("checked", false);
			}
			form.render();
		});

		form.on("checkbox", function(data) {
			var length = 0; //总长度
			var checkLength = 0;
			$(".childrenBox input[type='checkbox']").each(function() {
				length++;
				if($(this).is(":checked")) {
					checkLength++;
				}

			});
			if(checkLength == length) {
				$(".parentBox").prop("checked", true);
			} else {
				$(".parentBox").prop("checked", false);
			}
			form.render();
		});

		$(".batchDelete").click(function() {
			var ids = "";
			var check = 0;
			$(".childrenBox input[type='checkbox']").each(function() {
				if($(this).is(":checked")) {
					ids += $(this).attr("user_id") + ",";
					check++;
				}

			});

			if(check > 0) {
				layer.confirm("你确定要删除" + check + "条记录", function() {
					delete_userbByUserId(ids.substring(0, ids.length - 1));
				});
			} else {
				layer.msg("不能删除少于0条记录", {
					icon: 2,
					time: 2000
				});

			}
		});
		//账号的开启与禁封
		form.on("switch", function(data) {
			var userId = data.elem.getAttribute("user_id");
			change_state(userId);
		});

		function change_state(userId) {
			$.ajax({
				type: "get",
				url: "http://localhost:8080/ssmService/sysUser/tiggerState/" + userId,
				async: true,
				"success": function(result) {
					if(result.resultCode == 0) {
						layer.msg(result.message, {
							icon: 1,
							time: 1000
						});
					}
				},
				"error": function() {
					layer.msg("状态切换失败,请联系管理员", {
						icon: 2,
						time: 1000
					});
				}
			});
		}
		//重置密码
		$(document).on("click",".reset",function(){
			var userId=$(this).attr("user_id");
			reset_password(userId);
		});
		function reset_password(userId){
				$.ajax({
				type: "get",
				url: "http://localhost:8080/ssmService/sysUser/resetPassword/"+userId,
				async: true,
				"success": function(result) {
					if(result.resultCode == 0) {
						layer.msg(result.message, {
							icon: 1,
							time: 1000
						});
					}
				},
				"error": function() {
				layer.msg("重置密码失败,请联系管理员", {
						icon: 2,
						time: 1000
					});
				}
			});
		}
		$(".addUser").click(function() {
			var index = layer.open({
				type: 2,
				content: 'insertuser.html',
				title: false,
				maxmin: true,
				closeBtn: 1
			});
			layer.full(index);
		});
		$(document).on("click",".updateUser",function(){
			var userId=$(this).attr("user_id");
			var index = layer.open({
				type: 2,
				content: 'EditorUser.html?userId='+userId,
				title: false,
				maxmin: true,
				closeBtn: 1
				});
			layer.full(index);
		});
	
	});
});